//index.js 
//获取应用实例 
const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
Page({
    data: {
        userList: [],
        propArray: [],
        isRoutingStaticMenus: 0,
        activeKey: 0,
        background: "#f2f2f2",
        year: '',
        month: '',
        taskType: '',
        marketTaskType: 1,
        isHandle: "0",
        provinceId: "",
        hosPatrolList: [],
        maintenancetasks: [],
        longTaskList: [],
        cityId: "",
        city: '',
        taskCity: [1, 2, 3, , , , , ],
        grade: '',
        page: "1",
        searchText: '',
        loading: false,
        loaded: true,
        isBackBtn: true, // 设置是否显示回到顶部按钮
        defaultSetting: {
            shake: true, // 设置是否开启震动
            style: "black", // 设置圆点申诉还是浅色
        },
        beforeClose: function (action) {
            if (action == 'cancel') {
                return true
            } else {
                return false
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */

    onLoad: function (e) {
        var that = this;
        that.setData({
            year: new Date().getFullYear(),
            month: (new Date().getMonth() + 1) >= 10 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1),
            taskType: e.taskType
        })
        that.getWorker();
        that.getProjectList("sign_in_range_time")
    },
    //获取签到时间段
    getProjectList(code) {
        let that = this
        lbbService.getERPDictByCode({
            code: code
        }, function (res) {
            that.setData({
                signRangeTime: res.data
            })
        })
    },
    //搜索的关键字
    onChangeValue(e) {
        this.setData({
            searchText: e.detail
        })
    },
    //关键字搜索
    onSearch() {
        let that = this
        that.setData({
            longTaskList: [],
            maintenancetasks: [],
            city: '',
            grade: '',
            page: 1
        })
        that.getTaskList()
        // if (this.data.taskType == 1 && this.data.marketTaskType == 1) {
        //     let hosList = this.query(this.data.temporaryMarketTasks)
        //     this.setData({
        //         maintenancetasks: hosList
        //     })
        // } else if (this.data.taskType == 1 && this.data.marketTaskType == 0) {
        //     let hosList = this.query(this.data.longTaskListOriginal)
        //     this.setData({
        //         longTaskList: hosList
        //     })
        // }
    },
    query(list) {
        let that = this
        return list.filter(item => item.hospitalName.indexOf(that.data.searchText ?? '') != -1)
    },
    //获取基本配置
    getBaseConfig: function () {
        let that = this;
        lbbService.getBaseConfig({}, res => {
            console.log('重制')
            util.print('config', res);
            app.globalData.config = res;
            that.setData({
                isRoutingStaticMenus: res.isRoutingStaticMenus
            })
        })
    },
    //获取师傅
    getWorker: function () {
        let that = this;
        lbbService.getWorker({
            taskType: that.data.taskType
        }, function (res) {
            console.log(89898, res)
            if (res.data.length == 1) {
                that.setData({
                    userId: res.data[0].id,
                    userName: res.data[0].name
                })
            } else {
                wx.getUserInfo({
                    success(res) {
                        that.setData({
                            userName: res.userInfo.nickName
                        })
                    }
                })
            }
            that.getTaskList()
            that.setData({
                userList: res.data
            })
        })
    },
    //获取省市
    getCitys: function () {
        let that = this;
        lbbService.getCitys({}, function (res) {
            that.setData({
                propArray: res
            })
            that.getTaskList()
        })
    },
    //获取医院任务列表
    getTaskList: function () {
        let that = this
        let date = new Date()
        let month = date.getMonth() + 1
        let year = new Date().getFullYear()
        if (that.data.taskType == 0) {
            lbbService.getHospitalTaskListv2({
                month: that.data.month.toString(),
                year: that.data.year.toString(),
                isHandle: that.data.isHandle,
                provinceId: that.data.provinceId,
                cityId: that.data.cityId,
                userId: that.data.userId,
                taskType: that.data.taskType,
                pageNo: that.data.page,
                pageSize: "10",
                city: that.data.city,
            }, function (res) {
                let lists = [...that.data.hosPatrolList, ...res.data.records]
                if (that.data.city && lists.length == 0) {
                    that.setData({
                        city: '',
                        grade: '',
                    })
                    that.getTaskList()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        hosPatrolList: lists,
                        hosPatrolListOriginal: JSON.parse(JSON.stringify(lists)),
                        TotalPage: res.data.pages
                    })
                }

            })
            lbbService.cityStatistics({
                month: that.data.month.toString(),
                year: that.data.year.toString(),
                isHandle: that.data.isHandle,
                provinceId: that.data.provinceId,
                cityId: that.data.cityId,
                userId: that.data.userId,
                taskType: that.data.taskType,
                pageNo: that.data.page,
                pageSize: "10",
                city: that.data.city,
            }, function (res) {
                console.log('有任务医院', res.data)
                that.setData({
                    taskCity: res.data
                })
            })
        } else if (that.data.taskType == 1) {
            if (that.data.marketTaskType == 0) {
                // 长期任务
                lbbService.getLongTaskList({
                    userId: that.data.userId,
                    provinceId: that.data.provinceId,
                    cityId: that.data.cityId,
                    time: that.data.year.toString() + '-' + that.data.month.toString(),
                    taskType: that.data.taskType,
                    isHandle: that.data.isHandle,
                    keywords: that.data.searchText,
                    pageNo: that.data.page,
                    pageSize: "10",
                    city: that.data.city,
                }, function (res) {
                    for (let i = 0; i < res.data.records.length; i++) {
                        if (res.data.records[i].operationList) {
                            let operationTypeCodeList = []
                            res.data.records[i].operationList.forEach(item => {
                                operationTypeCodeList.push(item.operationTypeCode)
                            })
                            res.data.records[i].operationTypeCodeList = operationTypeCodeList
                        }
                    }
                    console.log('市场长期任务-------->', res.data)
                    let lists = [...that.data.longTaskList, ...res.data.records]
                    that.setData({
                        loading: false,
                        loaded: true,
                        longTaskList: lists,
                        longTaskListOriginal: JSON.parse(JSON.stringify(lists)),
                        TotalPage: res.data.pages
                    })
                })
            } else if (that.data.marketTaskType == 1) {
                // 临时任务
                lbbService.getTemporaryTaskList({
                    userId: that.data.userId,
                    provinceId: that.data.provinceId,
                    cityId: that.data.cityId,
                    time: that.data.year.toString() + '-' + that.data.month.toString(),
                    taskType: that.data.taskType,
                    isHandle: that.data.isHandle,
                    keywords: that.data.searchText,
                    pageNo: that.data.page,
                    pageSize: "10",
                    city: that.data.city,
                }, function (res) {
                    for (let i = 0; i < res.data.records.length; i++) {
                        if (res.data.records[i].taskImages) {
                            res.data.records[i].taskImages = res.data.records[i].taskImages.split(',')
                        }
                        if (res.data.records[i].operationList) {
                            let operationTypeCodeList = []
                            res.data.records[i].operationList.forEach(item => {
                                operationTypeCodeList.push(item.operationTypeCode)
                            })
                            res.data.records[i].operationTypeCodeList = operationTypeCodeList
                        }

                        if (res.data.records[i].endTime) {
                            if (res.data.records[i].endTime.split(' ')[0] > res.data.records[i].handleRuleTime) {
                                res.data.records[i]['state'] = '逾期完成'
                            } else {
                                res.data.records[i]['state'] = '已完成'
                            }
                        }
                    }

                    console.log('市场临时任务--------->', res.data)
                    let lists = [...that.data.maintenancetasks, ...res.data.records]
                    that.setData({
                        loading: false,
                        loaded: true,
                        maintenancetasks: lists,
                        temporaryMarketTasks: JSON.parse(JSON.stringify(lists)),
                        TotalPage: res.data.pages
                    })
                    console.log('任务状态--------->', that.data.marketTaskType)
                })
            }
            lbbService.cityStatistics({
                month: that.data.month.toString(),
                year: that.data.year.toString(),
                isHandle: that.data.isHandle,
                provinceId: that.data.provinceId,
                cityId: that.data.cityId,
                userId: that.data.userId,
                taskType: that.data.taskType,
                isLongTask: that.data.marketTaskType == 0 ? 1 : 0,
                keywords: that.data.searchText,
                pageNo: that.data.page,
                pageSize: "10",
                city: that.data.city,
            }, function (res) {
                console.log('有任务医院', res.data)
                that.setData({
                    taskCity: res.data
                })
            })
        }

    },
    cityDetail: function (e) {
        console.log('525858', e.target.dataset.cityname)
        let that = this
        that.setData({
            grade: e.target.dataset.cityname,
            page: '1',
            hosPatrolList: [],
            longTaskList: [],
            maintenancetasks: [],
            city: e.target.dataset.cityname
        })
        that.getTaskList()
    },
    //市场临时任务完成
    async goHandle(e) {
        console.log('去处理------>', e)
        let that = this
        // await that.getWXLocation(that)
        that.setData({
            signOrachieve: 'achieve',
            taskId: e.currentTarget.dataset.id,
        })
        if (e.currentTarget.dataset.issignin) {
            if (e.currentTarget.dataset.operationlist?.length <= 0) {
                await that.getWXLocation(that)
                if (e.currentTarget.dataset.tasktype=='long') {
                    lbbService.TaskItemsOk({
                        taskLongStateId: e.currentTarget.dataset.id,
                        handleLocation: that.data.addressDetail
                    }, function (res) {
                        if (that.data.longTaskList.length == 1) {
                            that.setData({
                                city: '',
                                grade: '',
                            })
                        }
                        that.setData({
                            page: '1',
                            longTaskList: []
                        })
                        that.getTaskList()
                        // that.getTaskListNumber()
                    })
                } else {
                    lbbService.HandleOk({
                        taskDevOpsId: that.data.taskId,
                        handleDesc: that.data.textArea ? that.data.textArea : '',
                        handleLocation: that.data.addressDetail
                    }, function (res) {
                        if (that.data.maintenancetasks.length == 1) {
                            that.setData({
                                city: '',
                                grade: '',
                            })
                        }
                        that.setData({
                            page: '1',
                            hosPatrolList: [],
                            longTaskList: [],
                            maintenancetasks: []
                        })
                        that.getTaskList()
                        that.setData({
                            textArea: null,
                            showFrom: false
                        })
                    })
                }
            } else {
                that.setData({
                    showFrom: true,
                    selectHospitalId: e.currentTarget.dataset.hospitalid,
                    configOperationlist: e.currentTarget.dataset.operationlist,
                    selectTasktype: e.currentTarget.dataset.tasktype,
                })
            }
        } else {
            wx.showToast({
                title: '请先完成签到',
                icon: 'error',
                duration: 1000
            })
        }

    },
    //点击完成任务的弹窗列表
    goOperatePage(e) {
        console.log(e,111111)
        let operationTypeCode = e.currentTarget.dataset.operationtypecode
        let taskid = e.currentTarget.dataset.taskid
        if(this.data.selectTasktype=='long'){
            this.counterPageLong(operationTypeCode, taskid)
        }else{
            this.counterPage(operationTypeCode, taskid)
        }
    },
    //去未操作提交页面的判断
    counterPage(code, taskid) {
        let configOperationlist = JSON.stringify(this.data.configOperationlist)
        if (code == 'B19A03A01A01') {
            wx.navigateTo({
                url: `/pkgTask/pages/prebid-communication/index?taskId=${taskid}&type=${'pre_bid_communication'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A01`
            })
        } else if (code == 'B19A03A01A02') {
            wx.navigateTo({
                url: `/pkgTask/pages/Internal-bidding/index?taskId=${taskid}&type=${'in_invite_bid'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A02`
            })
        } else if (code == 'B19A03A01A03') {
            wx.navigateTo({
                url: `/pkgTask/pages/Internal-bidding/index?taskId=${taskid}&type=${'assist_invite_bid'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A03`
            })
        } else if (code == 'B19A03A01A04') {
            wx.navigateTo({
                url: `/pkgTask/pages/hospital-Information/index?taskId=${taskid}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A04`
            })
        } else if (code == 'B19A03A01A05') {
            wx.navigateTo({
                url: `/pkgTask/pages/agent-link/index?taskId=${taskid}&type=${'agent_communication'}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A05`
            })
        } else if (code == 'B19A03A01A06') {
            wx.navigateTo({
                url: `/pkgTask/pages/prebid-communication/index?taskId=${taskid}&type=${'hospital_communication'}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A06`
            })
        } else if (code == 'B19A03A01A07') {
            wx.navigateTo({
                url: `/pkgTask/pages/look-site/index?taskId=${taskid}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A07`
            })
        } else if (code == 'B19A03A01A08') {
            wx.navigateTo({
                url: `/pkgTask/pages/usage-report/index?taskId=${taskid}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A08`
            })
        } else if (code == 'B19A03A01A09') {
            wx.navigateTo({
                url: `/pkgTask/pages/agent-information/index?taskId=${taskid}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A09`
            })
        } else if (code == 'B19A03A01A10') {
            wx.navigateTo({
                url: `/pkgTask/pages/agent-link/index?taskId=${taskid}&type=${'handle_contract'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A10`
            })
        } else if (code == 'B19A03A01A11') {
            wx.navigateTo({
                url: `/pkgTask/pages/agent-link/index?taskId=${taskid}&type=${'agent_urgent_communication'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A11`
            })
        } else if (code == 'B19A03A01A12') {
            wx.navigateTo({
                url: `/pkgTask/pages/prebid-communication/index?taskId=${taskid}&type=${'hospital_urgent_communication'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&operationTypeCode=B19A03A01A12`
            })
        }
    },
    counterPageLong(code, taskid) {
        let that=this
        let configOperationlist = JSON.stringify(this.data.configOperationlist)
        if (code == 'B19A04A01A01') {
            wx.navigateTo({
                url: `/pkgTask/pages/prebid-communication/index?taskId=${taskid}&type=${'pre_bid_communication'}&hospitalId=${this.data.selectHospitalId}&operationlist=${configOperationlist}&taskType=${this.data.selectTasktype}&activeKey=${that.data.activeKey}&operationTypeCode=B19A04A01A01`
            })
        } else if (code == 'B19A04A01A05') {
            wx.navigateTo({
                url: `/pkgTask/pages/maintenance-status/index?taskId=${taskid}&type=${'agent_relationship'}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&activeKey=${that.data.activeKey}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A05`
            })
        } else if (code == 'B19A04A01A03') {
            wx.navigateTo({
                url: `/pkgTask/pages/hospital-Information/index?taskId=${taskid}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&activeKey=${that.data.activeKey}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A03`
            })
        } else if (code == 'B19A04A01A02') {
            wx.navigateTo({
                url: `/pkgTask/pages/agent-link/index?taskId=${taskid}&type=${'agent_communication'}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&operationlist=${configOperationlist}&activeKey=${that.data.activeKey}&operationTypeCode=B19A04A01A02`
            })
        } else if (code == 'B19A04A01A06') {
            wx.navigateTo({
                    url: `/pkgTask/pages/maintenance-status/index?taskId=${taskid}&type=${'hospital_relationship'}&hospitalId=${this.data.selectHospitalId}&taskType=${this.data.selectTasktype}&activeKey=${that.data.activeKey}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A06`
            })
        } else if (code == 'B19A04A01A07') {
            wx.navigateTo({
                url: `/pkgTask/pages/speaker-communication/index?taskId=${taskid}&hospitalId=${this.data.selectHospitalId}&activeKey=${that.data.activeKey}&taskType=${this.data.selectTasktype}&operationlist=${configOperationlist}&operationTypeCode=B19A04A01A07`
            })
        } 
    },
    // 输入框内容
    bindAreaChange(e) {
        let that = this
        that.setData({
            textArea: e.detail.value
        })
    },
    // 输入框确定按钮
    okRemarks() {
        let that = this
        console.log('3333', this.data.textArea, that.data.taskId)
        lbbService.HandleOk({
            taskDevOpsId: that.data.taskId,
            handleDesc: that.data.textArea ? that.data.textArea : '',
            handleLocation: that.data.addressDetail
        }, function (res) {
            if (that.data.maintenancetasks.length == 1) {
                that.setData({
                    city: '',
                    grade: '',
                })
            }
            that.setData({
                page: '1',
                hosPatrolList: [],
                longTaskList: [],
                maintenancetasks: []
            })
            that.getTaskList()
            that.setData({
                textArea: null,
                showFrom: false
            })
        })
    },
    //市场长期任务点击完成
    tocomplete: async function (e) {
        let that = this
        that.setData({
            // showFrom: true,
            // taskId: e.currentTarget.dataset.id,
            signOrachieve: 'achieve'
        })
        await that.getWXLocation(that)
        wx.showModal({
            title: '完成',
            content: `您确定完成${e.currentTarget.dataset.name}吗？`,
            showCancel: true,
            confirmText: '确定',
            success(res) {
                if (res.confirm) {
                    lbbService.TaskItemsOk({
                        taskLongStateId: e.currentTarget.dataset.id,
                        handleLocation: that.data.addressDetail
                    }, function (res) {
                        if (that.data.longTaskList.length == 1) {
                            that.setData({
                                city: '',
                                grade: '',
                            })
                        }
                        that.setData({
                            page: '1',
                            longTaskList: []
                        })
                        that.getTaskList()
                        // that.getTaskListNumber()
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // if(this.data.hosPatrolList.length==1){
        //     that.setData({
        //         city: '',
        //         grade: '0',
        //     }) 
        // }
        this.setData({
            hosPatrolList: [],
            longTaskList: [],
            maintenancetasks: [],
            page: '1',
            showFrom: false
        })
        this.getTaskList();

    },
    //切换侧边导航栏
    changeStat(e) {
        let that = this
        let month = (new Date().getMonth() + 1) >= 10 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1)
        let year = new Date().getFullYear()
        that.setData({
            hosPatrolList: [],
            longTaskList: [],
            maintenancetasks: [],
        })
        if (e.detail == "0") {
            that.setData({
                year: year,
                month: month,
                isHandle: "0",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        } else if (e.detail == "1") {
            let Lmonth = (new Date().getMonth() + 2) >= 10 ? new Date().getMonth() + 2 : '0' + (new Date().getMonth() + 2)
            let Lyear = new Date().getFullYear()
            if (Lmonth == 13) {
                Lmonth = 1
                Lyear = Lyear + 1
            }
            that.setData({
                month: Lmonth,
                year: Lyear,
                isHandle: "0",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        } else if (e.detail == "2") {
            let upperMonth = new Date().getMonth() >= 10 ? new Date().getMonth() : '0' + new Date().getMonth()
            let upperYear = new Date().getFullYear()
            if (upperMonth == 0) {
                upperMonth = 12
                upperYear = upperYear - 1
            }
            that.setData({
                month: upperMonth,
                year: upperYear,
                isHandle: "0",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        } else if (e.detail == "3") {
            that.setData({
                month: month,
                year: year,
                isHandle: "1",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        }

    },
    //事件处理函数
    myEventListener: function (e) {
        console.log("myEventListener>>", e)
        let that = this
        that.setData({
            provinceId: e.detail[0] == null ? "" : e.detail[0],
            cityId: e.detail[1] == null || e.detail[1] == -1 ? "" : e.detail[1],
            month: e.detail[2] == null || e.detail[2] == -1 ? that.data.month : e.detail[2],
            userId: e.detail[5] == null || e.detail[5] == 0 ? that.data.userId : e.detail[5],
            marketTaskType: e.detail[6] == null || e.detail[6] == -1 ? that.data.marketTaskType : e.detail[6],
            page: '1',
            city: '',
            grade: '',
            hosPatrolList: [],
            longTaskList: [],
            maintenancetasks: []
        })
        that.getTaskList()
    },
    //上拉加载更多
    eventhandle(e) {
        let that = this
        let pagenum = Number(that.data.page)
        if (pagenum >= that.data.TotalPage) {
            that.setData({
                loading: false,
                loaded: true
            })
        } else {
            pagenum = pagenum + 1
            that.setData({
                loading: true,
                loaded: false
            })
            that.setData({
                page: pagenum.toString()
            })
            that.getTaskList()
        }
    },

    //点击去处理
    goNavigator(e) {

    },
    //签到
    async signIn(e) {
        let that = this;
        that.setData({
            hospitalId: e.currentTarget.dataset.hospitalid,
            index: e.currentTarget.dataset.index,
            placeType: e.currentTarget.dataset.placetype
        })
        let location = null;
        try {
            location = await that.getWXLocation(that)
        } catch (err) {}
    },
    //点击签到获取当前位置
    getWXLocation: (thats) => {
        wx.showLoading({
            title: '定位中...',
            mask: true,
        })
        return new Promise((resolve, reject) => {
            let _locationChangeFn = (res) => {
                resolve(res);
                thats.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
                thats.getAddress()
                wx.hideLoading()
                wx.offLocationChangeError(_locationChangeFn)
                wx.offLocationChange(_locationChangeFn)
                wx.stopLocationUpdate()
            }
            wx.startLocationUpdate({
                success: (res) => {
                    wx.onLocationChangeError(_locationChangeFn)
                    wx.onLocationChange(_locationChangeFn)
                },
                fail: (err) => {
                    wx.hideLoading()
                    reject()
                    thats.getAddress()
                }
            })
        })
    },
    //查看签到
    checkIn: function (e) {
        lbbService.getSignInInfo({
            mechanismId: e.currentTarget.dataset.hospitalid,
            userId: this.data.userId,
            placeType: e.currentTarget.dataset.placetype
        }, function (res) {
            wx.showModal({
                title: '签到信息',
                content: '签到时间：' + res.data.signInDate + '\r\n' + '签到地点：' + res.data.location,
                showCancel: false,
            })
        })
    },
    //运维签到接口
    maintenanceSignIn(address) {
        let that = this
        wx.showModal({
            title: '当前位置',
            content: address,
            success(res) {
                if (res.confirm) {
                    lbbService.signIn({
                        mechanismId: that.data.hospitalId,
                        placeType: that.data.placeType,
                        latitude: that.data.latitude,
                        longitude: that.data.longitude,
                        location: address,
                        userId: that.data.userId,
                        forceSubmit:0
                    }, function (res) {
                        console.log('签到', res)
                        if (res.result == 0) {
                            if(res.data==-10) {
                                that.setData({
                                    showOperation:true
                                })
                            } else {
                                that.data.hosPatrolList[that.data.index].isSignIn = 1
                                that.setData({
                                    hosPatrolList: [...that.data.hosPatrolList, ]
                                })
                                wx.showToast({
                                    title: '签到成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    //重新确认签到
    okOperationSignIn() {
        let that = this
        lbbService.signIn({
            mechanismId: that.data.hospitalId,
            placeType: that.data.placeType,
            latitude: that.data.latitude,
            longitude: that.data.longitude,
            location: that.data.addressDetail,
            userId: that.data.userId,
            forceSubmit:1
        }, function (res) {
            console.log('签到', res)
            if (res.result == 0) {
                that.data.hosPatrolList[that.data.index].isSignIn = 1
                that.setData({
                    hosPatrolList: [...that.data.hosPatrolList, ],
                    showOperation:false
                })
                wx.showToast({
                    title: '签到成功',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
    }, 
    //市场任务签到获取当前位置
    async marketTemporarySignIn(e) {
        let that = this
        that.setData({
            marketTaskid: e.currentTarget.dataset.taskid,
            marketIndex: e.currentTarget.dataset.index,
            signOrachieve: 'sign'
        })
        let location = null;
        try {
            location = await that.getWXLocation(that)
        } catch (err) {}
    },
    //完成任务签到
    okSignIn() {
        let that = this
        console.log(that.data.remImages)
        if (!that.data.remImages || that.data.remImages?.length <= 0) {
            wx.showToast({
                title: '请上传图片！',
                icon: 'error',
                duration: 1000
            })
            return
        }
        that.marketTemporarySignInExecute(that.data.addressDetail, that.data.marketSignInText, that.data.remImages)
    },
    //签到备注
    marketSignInAreaChange(e) {
        this.setData({
            marketSignInText: e.detail.value
        })
    },
    //上传图片
    setImgArr(e) {
        let that = this
        let imgArray = e.detail;
        let newUpLoadImg = []
        let oldUpLoadImg = []
        imgArray.forEach(item => {
            if (item.indexOf('http://tmp/') != -1 || item.indexOf('wxfile://tmp') != -1) {
                newUpLoadImg.push(item)
            } else {
                oldUpLoadImg.push(item)
            }
        })
        util.newUpLoadImg(app.globalData.header, newUpLoadImg, 0, [], function (imgIdArray) {
            console.log(8888,imgIdArray)
            oldUpLoadImg.push(...imgIdArray)
            that.setData({
                remImages: oldUpLoadImg
            })
        });
    },
    //取消任务签到
    cancelSignIn() {
        let that = this
        // that.setData({
        //     showSignInFrom: false
        // })
    },
    //市场任务签到接口
    marketTemporarySignInExecute(address, marketRemark, remImages) {
        let that = this
        lbbService.taskMarketSignIn({
            taskId: that.data.marketTaskid,
            taskType: that.data.marketTaskType == 0 ? 1 : 0,
            latitude: that.data.latitude,
            longitude: that.data.longitude,
            location: address,
            remark: marketRemark ? marketRemark : '',
            images: remImages ? remImages.toString() : ''
        }, function (res) {
            console.log('市场签到回调', res, that.data.maintenancetasks)
            if (that.data.marketTaskType == 0) {
                that.data.longTaskList[that.data.marketIndex].isSignIn = 1
                that.setData({
                    showSignInFrom: false,
                    longTaskList: that.data.longTaskList
                })
            } else {
                that.data.maintenancetasks[that.data.marketIndex].isSignIn = 1
                that.setData({
                    showSignInFrom: false,
                    maintenancetasks: that.data.maintenancetasks,
                })
            }

        })
    },
    //市场任务查看签到
    marketTemporaryCheckIn(e) {
        let that = this
        lbbService.taskMarketSignInInfo({
            taskId: e.currentTarget.dataset.taskid,
            taskType: that.data.marketTaskType == 0 ? 1 : 0
        }, function (res) {
            if (res.data.images) {
                res.data.images = res.data.images.split(',')
            }
            that.setData({
                showCheckIn: true,
                signInData: res.data
            })
            // wx.showModal({
            //     title: '签到信息',
            //     content: '签到时间：' + res.data.signInDate + '\r\n' + '签到地点：' + res.data.location + '\r\n' + '签到备注：' + `${res.data.remark?res.data.remark:'无'}`,
            //     showCancel: false,
            // })
        })
    },
    //根据经纬度获取详细地址
    getAddress: function () {
        let that = this
        wx.request({
            url: 'https://api.map.baidu.com/geocoder/v2/',
            method: 'GET',
            data: {
                location: that.data.latitude + ',' + that.data.longitude,
                output: 'json',
                coordtype: 'wgs84ll',
                pois: 1,
                s: 1,
                latest_admin: 1,
                ak: 'GQKwKHdtu8CgmxXIkXdyTkdtlfvML4Gg'
            },
            success(res) {
                console.log('>>', res)
                if (res.data.status == 0 && res.data.result.formatted_address) {
                    let address = res.data.result.formatted_address
                    that.setData({
                        addressDetail: address
                    })
                    if (that.data.taskType == 0) {
                        that.maintenanceSignIn(address)
                    } else if (that.data.taskType == 1 && that.data.signOrachieve == 'sign') {
                        that.setData({
                            showSignInFrom: true,
                            marketSignInText: '',
                            remImages: []
                        })
                    }
                } else {
                    wx.showToast({
                        title: '获取位置失败,请打开手机定位重新签到',
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            fail() {
                that.setData({
                    address: null,
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },
    //图片预览
    preViewImgs(e) {
        let current = e.currentTarget.dataset.current
        let urls = e.currentTarget.dataset.imglist
        util.previewImgs(current, urls);
    },
    /**
     * 市场任务完成后操作
     */
    //标前沟通情况
    btnPreBidInteract(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${e.currentTarget.dataset.taskid}&type=${'pre_bid_communication'}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //院内招标说标
    btnInternalBidding(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/Internal-bidding/index?taskId=${e.currentTarget.dataset.taskid}&type=${'in_invite_bid'}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //代理商招标说标
    btnAgentBidding(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/Internal-bidding/index?taskId=${e.currentTarget.dataset.taskid}&type=${'assist_invite_bid'}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //医院沟通
    btnHospitalLink(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            if (e.currentTarget.dataset.tasktype == 'long') {
                that.data.longTaskList[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
                that.setData({
                    longTaskList: that.data.longTaskList
                })
            } else {
                that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
                that.setData({
                    maintenancetasks: that.data.maintenancetasks
                })
            }

        })
        wx.navigateTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${e.currentTarget.dataset.taskid}&type=${'hospital_communication'}&hospitalId=${e.currentTarget.dataset.hospitalid}&taskType=${e.currentTarget.dataset.tasktype}&activeKey=${that.data.activeKey}`
        })
    },
    //医院关系维护
    btnHospitalDefend(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.longTaskList[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                longTaskList: that.data.longTaskList
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/maintenance-status/index?taskId=${e.currentTarget.dataset.taskid}&type=${'hospital_relationship'}&hospitalId=${e.currentTarget.dataset.hospitalid}&taskType=${e.currentTarget.dataset.tasktype}&activeKey=${that.data.activeKey}`
        })
    },
    //代理商沟通
    btnAgentLink(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            if (e.currentTarget.dataset.tasktype == 'long') {
                that.data.longTaskList[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
                that.setData({
                    longTaskList: that.data.longTaskList
                })
            } else {
                that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
                that.setData({
                    maintenancetasks: that.data.maintenancetasks
                })
            }

        })
        wx.navigateTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${e.currentTarget.dataset.taskid}&type=${'agent_communication'}&hospitalId=${e.currentTarget.dataset.hospitalid}&taskType=${e.currentTarget.dataset.tasktype}&activeKey=${that.data.activeKey}`
        })
    },
    //代理商关系维护
    btnAgentDefend(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.longTaskList[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                longTaskList: that.data.longTaskList
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/maintenance-status/index?taskId=${e.currentTarget.dataset.taskid}&type=${'agent_relationship'}&hospitalId=${e.currentTarget.dataset.hospitalid}&taskType=${e.currentTarget.dataset.tasktype}&activeKey=${that.data.activeKey}`
        })
    },
    //代理商紧急情况沟通
    btnAgentUrgentLink(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${e.currentTarget.dataset.taskid}&type=${'agent_urgent_communication'}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //医院要求紧急拜访
    btnHospitalVisits(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/prebid-communication/index?taskId=${e.currentTarget.dataset.taskid}&type=${'hospital_urgent_communication'}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //看场地
    btnSite(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/look-site/index?taskId=${e.currentTarget.dataset.taskid}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //使用汇报
    btnUsage(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/usage-report/index?taskId=${e.currentTarget.dataset.taskid}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //合同处理
    btnContract(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/agent-link/index?taskId=${e.currentTarget.dataset.taskid}&type=${'handle_contract'}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //采集友商说标人信息
    btnFriendWord(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                maintenancetasks: that.data.maintenancetasks
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/agent-information/index?taskId=${e.currentTarget.dataset.taskid}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    //采集医院信息
    btnHospitalWord(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            if (e.currentTarget.dataset.tasktype == 'long') {
                that.data.longTaskList[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
                that.setData({
                    longTaskList: that.data.longTaskList
                })
            } else {
                that.data.maintenancetasks[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
                that.setData({
                    maintenancetasks: that.data.maintenancetasks
                })
            }
        })
        wx.navigateTo({
            url: `/pkgTask/pages/hospital-Information/index?taskId=${e.currentTarget.dataset.taskid}&hospitalId=${e.currentTarget.dataset.hospitalid}&taskType=${e.currentTarget.dataset.tasktype}&activeKey=${that.data.activeKey}`
        })
    },
    //友商说标人沟通情况
    btnCommunicationWordSpeaker(e) {
        let that = this
        lbbService.operationClick({
            taskId: e.currentTarget.dataset.taskid,
            operationType: e.currentTarget.dataset.operationtype
        }, function () {
            that.data.longTaskList[e.currentTarget.dataset.index].operationList[e.currentTarget.dataset.indext].clickStatus = 1
            that.setData({
                longTaskList: that.data.longTaskList
            })
        })
        wx.navigateTo({
            url: `/pkgTask/pages/speaker-communication/index?taskId=${e.currentTarget.dataset.taskid}&hospitalId=${e.currentTarget.dataset.hospitalid}&activeKey=${that.data.activeKey}`
        })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // wx.switchTab({
        //     url: '../hospitalList/hospitalList',
        //   })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

})